/***********************************
*
* author:huanglei
* mail:code.huanglei@gmail.com
* version:0.1
*
***********************************/

function ATuanZhang(){
	this.API_URL = 'http://www.atuanzhang.com';
}


ATuanZhang.prototype = {
	$: function(value){
		if (value.indexOf('.') == 0){
			return document.getElementsByClassName(value.substring(1));
		} else if (value.indexOf('#') == 0){
			return parent.document.getElementById(value.substring(1));
		} else {
			return document.getElementsByTagName(value);
		}
	},
	filter:	function(dom){
		var website = dom.getAttribute('value');
		var goodsArr = this.$('.atuanzhang-goods');
		this.$('.atuanzhuang-website-title')[0].value=website;
		goodsArr.each(function(goodsDom){
			if (goodsDom.website == website || website == '全部' ){
				goodsDom.style.display = '';
			} else {
				goodsDom.style.display = 'none';
			}
		});
 
	},
	init: function(){
		this.loadCity();
		//this.loadGoods('');
		this.initEvent();
	},
	initEvent:function(){
		this.$('#atuanzhang-cityTitleWrapper').addEventListener('mouseover', this.showCity, false);
		this.$('#atuanzhang-cityBgWrapper').addEventListener('mouseover', this.showCity, false);
		this.$('#atuanzhang-cityBgWrapper').addEventListener('mouseout', this.closeCity, false);
	},
	loadCity: function(){
		var me = this;	
		var url = this.API_URL+'/atuanzhang_api_citys';
		this.ajax(url,function(data){
			if (data){
				me.cityHandler(data);
			} else {
				dump("error");
			}
		},'json');
	},
	//load goods
	loadGoods: function(city){
		this.loading();
		var me = this;	
		var url = encodeURI(this.API_URL + '/atuanzhang_api_goods?city='+city+'&'+new Date().getTime());
		this.ajax(url,function(data){
			if (data){
				me.goodsHandler(data);
			} else {
				dump("error");
			}
			
			
		},'json');
	},
	goodsHandler: function(data){
		var p = this.$('#atuanzhang-goods-items');
		var goods = data.goods;
		var city = data.city;
		this.website =  new Array();
		this.removeAllChildren(ATuanZhang.$('#atuanzhuang-website'));
		this.removeAllChildren(p.childNodes[0]);
		for(var i=0; i<goods.length; i++){
			p.addGoods(i,goods[i]);
		}
		this.website.length = 0;
		this.$('#curCityDiv').value = city;
		this.$('#curCityDiv1').value = city;
		
		this.layer(0);
		
	},
	cityHandler: function(data){
		var hotCitys = data.hotCitys;
		var citys = data.citys;
		
		var p = this.$('#atuanzhang-cityDataWrapper');
		
		for(var i = 0; i<hotCitys.length; i++){
			p.addCity('atuanzhuang-hotCity',hotCitys[i]);
		}
		
		for(var i = 0; i<citys.length; i++){
			var py = citys[i].city_pinyin.substr(0,1);
			p.addCity('atuanzhuang-Citys',citys[i].city,py.toUpperCase());
		}
		
	},
	showCity: function(){
		ATuanZhang.$('#atuanzhang-cityBgWrapper').style.display='block';
	},
	closeCity: function(){
		ATuanZhang.$('#atuanzhang-cityBgWrapper').style.display='none';
	},
	showWebSite: function(){
	
	},
	/*UnSupported*/
	goodsMap: function(){
 			
	},
	popup: function(event){
		if (event.button == 0) {//left click
			this.$('#atuanzhang-panel').openPopup(this.$('#status-bar'), 'before_end');
			this.loadGoods('');
		}
	},
	openUrl: function (url){
		var wnd = window.top.getBrowser().selectedBrowser.contentWindow;
		wnd.open(url);
	},
	ajax: function(url,fnCallback,rtype){
		var type = rtype?rtype:'xml';
		var xhr = new XMLHttpRequest();
		xhr.onreadystatechange = function(){
			if (xhr.readyState == 4){
				if (type === 'xml'){
					fnCallback(xhr.responseXML);
				} else if (type === 'json'){
					fnCallback(eval("("+xhr.responseText+")"));
				} else {
					fnCallback(xhr.responseText);
				}
			}
		}; 
		xhr.open('GET',url,true);
		xhr.send();
	},
	removeAllChildren: function(value){
		while (value.firstChild){  
			value.removeChild(value.firstChild);  
		}  
	},
	loading: function(){
		this.layer(1);
	},
	layer: function(index){
		this.$('#atuanzhang-layer').selectedIndex = index;
	},
	log: function(msg) {
		var consoleService = Components.classes['@mozilla.org/consoleservice;1']
									 .getService(Components.interfaces.nsIConsoleService);
		consoleService.logStringMessage(msg);
	}
}



document.getElementsByClassName = function(className,elm) {  
    var testClass = new RegExp('(^|\s)' + className + '(\s|$)');  
    elm = elm || document
    var elements = elm.all? elm.all : elm.getElementsByTagName('*');  
    var returnElements = [];  
    var current;  
    var length = elements.length;  
    for(var i=0; i<length; i++){  
        current = elements[i];  
        if(testClass.test(current.className)){  
            returnElements.push(current);  
        }  
    }  
    return returnElements;  
}  

Array.prototype.each=function(f){
	try{
		for(var i=0;i<this.length;i++){
			try{
				f(this[i],i,this);
			}catch(e){}
		}
	}catch(e){}
}

Array.prototype.set = function(value){
	for(var i=0;i<this.length;i++){
		if(this[i] == value) return false;
	}
	
	this.push(value);
}


var ATuanZhang = new  ATuanZhang();


window.addEventListener('load', function(e){
	ATuanZhang.init();
}, false);




